package com.baselibrary.view

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View

class ProgressWithTextBar(context: Context, attrs: AttributeSet) : View(context, attrs) {

    private var progress = 0f
    private var text = ""
    private var progressColor = Color.BLUE
    private var textColor = Color.BLACK
    private var bgColor = Color.GRAY
    private var textSize = 20f

    private val paint = Paint().apply {
        style = Paint.Style.FILL
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val width = width.toFloat()
        val height = height.toFloat()
        val radius = height / 2

        // Draw background
        paint.color = bgColor
        canvas.drawRoundRect(RectF(0f, 0f, width, height), radius, radius, paint)

        // Draw progress bar
        paint.color = progressColor
//        HKLogUtils.e("画进度：${progress * width}")
        val rect = RectF(0f, 0f, progress * width, height)

        canvas.drawRoundRect(rect, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 6f, resources.displayMetrics), TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 6f, resources.displayMetrics), paint)

        // Draw text
        paint.color = textColor
        paint.textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, textSize, resources.displayMetrics)
        val textWidth = paint.measureText(text)
        val textX = if (progress == 1f) {
            progress * width.minus(textWidth.plus(textWidth.div(4)))
        } else if (progress >= 0.8f && progress < 1f) {
            progress * width.minus(textWidth.plus(textWidth.div(2)))
        } else {
            (progress * width).plus(textWidth.div(6f))
        }
        val textY = height / 2 - (paint.descent() + paint.ascent()) / 2
        canvas.drawText(text, textX, textY, paint)
    }

    fun setProgress(progress: Float) {//百分之一到五 都显示百分之五的进度 小于1% 都是0
//        this.progress = if (progress > 0 && progress < 0.01) (if (progress < 0.01) 0f else 0.05f) else progress
        this.progress = if (progress >= 0.05) progress else 0f
        invalidate()
    }

    fun setProgressWithAnimation(progress: Float) {
        setProgress(0f)
        val animator = ValueAnimator.ofFloat(0f, progress)
        animator.duration = 1000
        animator.addUpdateListener {
            val value = it.animatedValue as Float
            //%1的进度UI看起来很难看，所以显示3%
//            setProgress(if (value > 0 && value <= 0.03) 0.03f else value)
            setProgress(value)
        }
        animator.start()
    }

    fun setText(text: String) {
        this.text = "已抢${text}%"
        invalidate()
    }

    fun setProgressColor(color: Int) {
        this.progressColor = color
        invalidate()
    }

    fun setTextColor(color: Int) {
        this.textColor = color
        invalidate()
    }

    fun setBgColor(color: Int) {
        this.bgColor = color
        invalidate()
    }

    fun setTextSize(size: Float) {
        this.textSize = size
        invalidate()
    }
}
